package com.qf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.dto.DtsSerachDto;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.AdminService;
import com.qf.service.BrandService;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: shop-manage
 *
 * @author: 元
 * time: 2021/9/15 0015 20:34
 * description:商场管理:品牌制造商接口
 */
@RestController
@CrossOrigin("*")
@RequestMapping("brand")
public class BrandController {

    public static final Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Autowired
    private BrandService brandService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private CategoryService categoryService;

   //品牌制造商列表
    @RequestMapping("list")
    public Object list(Integer page, Integer limit,Integer id,String name,String sort, String order){

        IPage<DtsBrand> brandIPage = brandService.findByPage(page,limit,id,name,sort,order);

        Map<String, Object> data = new HashMap<>();
        data.put("total",brandIPage.getTotal());
        data.put("items",brandIPage.getRecords());
        return ResponseUtil.ok(data);
    }

    //品牌和管理员下拉框显示
    @RequestMapping("/catAndAdmin")
    public Object catAndAdmin() {

        Map<String,Object> data = new HashMap<>();
        //1.查询所需要的admin数据
        List<DtsAdmin> admins =  adminService.findAllAdmin();
        //2.查询所有1级类目
        List<DtsCategory> categorys = categoryService.selectLI();
        //3.将两组数据分别放入两个map中
        List<Map<String,Object>> adminList = new ArrayList<>();
        if(admins != null){
            for (DtsAdmin dtsAdmin: admins){
                Map<String,Object> adminMap = new HashMap<>();
                adminMap.put("value",dtsAdmin.getId());
                adminMap.put("label",dtsAdmin.getUsername());
                adminList.add(adminMap);
            }
        }

        //4.通过一个Map类型的list来封装categoryList
        List<Map<String,Object>> categoryList = new ArrayList<>();
        //5.循环遍历数据,并放入map中
        if (categorys != null){
            for(DtsCategory Category: categorys) {
                Map<String, Object> categoryMap = new HashMap<>();
                categoryMap.put("value", Category.getId());
                categoryMap.put("label", Category.getName());
                //6.根据此id=pid查询其子类,即1级类目下的二级类目
                List<DtsCategory> childrenList = categoryService.selectById(Category.getId());
                List<Map> childrens = new ArrayList<>();

                //7.将数据遍历放入一个map中
                if (childrenList != null){
                    for (DtsCategory children : childrenList) {
                        Map<String, Object> childrenMap = new HashMap<>();
                        childrenMap.put("value", children.getId());
                        childrenMap.put("label", children.getName());
                        childrens.add(childrenMap);
                    }
                    //8.将封装好的数据放入之前定义好的categoryMap中
                    categoryMap.put("childern", childrens);
                }
                //9.将返回categoryMap中的数据再放入定义的list集合中
                categoryList.add(categoryMap);
            }
    }
        //将封装好的数据放入最外层map中
        data.put("adminList",adminList);
        data.put("categoryList",categoryList);

        return ResponseUtil.ok(data);

    }

    //品牌制造商添加
    @RequestMapping("create")
    public Object create(@RequestBody DtsBrand dtsBrand ){
         brandService.addBrand(dtsBrand);
        return ResponseUtil.ok(dtsBrand);
    }


    //修改品牌制造商信息
    @RequestMapping("update")
    public Object update(@RequestBody DtsBrand dtsBrand){
        int rows = brandService.updateBrand(dtsBrand);
        //返回结果
        if (rows == 0){
            return ResponseUtil.fail();
        }
        return  ResponseUtil.ok(dtsBrand);

    }


    @RequestMapping("delete")
    public Object delete(@RequestBody DtsBrand dtsBrand){
        int rows = brandService.deleteById(dtsBrand.getId());
        if (rows == 0){
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok();
    }

}
